package com.resifind.www.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.resifind.www.constant.ApplicationStatus;
import com.resifind.www.constant.UserTypes;
import com.resifind.www.exception.BaseException;
import com.resifind.www.exception.Resp;
import com.resifind.www.po.*;
import com.resifind.www.service.*;
import com.resifind.www.service.impl.*;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

/**
 * @author 36069
 */
@SuppressWarnings({"unused", "RedundantThrows"})
@WebServlet("/merchant/*")
public class MerchantServlet extends BaseServlet {
    private final UserService userService = new UserServiceImpl();
    private final MerchantService merchantService = new MerchantServiceImpl();
    private final RoomService roomService = new RoomServiceImpl();
    private final OrderService orderService = new OrderServiceImpl();
    private final BlacklistFavourService blacklistFavourService = new BlacklistFavourServiceImpl();

    /**
     * 根据商家id查询商家房型信息
     */
    public void queryRoom(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json;charset=utf-8");
        String params = req.getReader().readLine();
        Integer merchantId = JSON.parseObject(params, int.class);
        try {
            Resp<List<Room>> listResp = roomService.queryRoomByMerchantId(merchantId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 修改商家信息
     */
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Merchant merchant = JSON.parseObject(params, Merchant.class);
        try {
            Resp<String> stringResp = merchantService.updateMerchant(merchant);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 修改房型信息
     */
    public void updateRoom(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Room room = JSON.parseObject(params, Room.class);
        try {
            Resp<String> stringResp = roomService.updateRoom(room);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 删除房型信息
     */
    public void deleteRoom(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer roomId = JSON.parseObject(params, int.class);
        try {
            Resp<String> stringResp = roomService.deleteRoom(roomId);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 添加房型
     */
    public void addRoom(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        Integer merchantId = root.getInteger("merchantId");
        Room room = root.getObject("room", Room.class);
        try {
            Resp<String> stringResp = roomService.addRoom(merchantId, room);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 查询订单信息
     */
    public void getOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        User user = JSON.parseObject(params, User.class);
        try {
            Resp<List<Order>> listResp = orderService.queryOrderByUserId(user.getId(), user.getUserType());
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 查询黑名单
     */
    public void getBlacklist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json;charset=utf-8");
        String params = req.getReader().readLine();
        Integer merchantId = JSON.parseObject(params, int.class);
        try {
            Resp<List<Client>> listResp = blacklistFavourService.queryBlacklist(merchantId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 删除黑名单
     */
    public void cancelBlack(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        Integer merchantId = root.getInteger("merchantId");
        List<Integer> clientIds = root.getJSONArray("clientIds").toJavaList(Integer.class);
        try {
            Resp<String> stringResp = blacklistFavourService.cancelBlack(merchantId, clientIds, UserTypes.MERCHANT);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 添加黑名单
     */
    public void addBlacklist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        Integer merchantId = root.getInteger("merchantId");
        Integer clientId = root.getInteger("clientId");
        try {
            Resp<String> stringResp = blacklistFavourService.blackClient(clientId, merchantId);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 提交申请
     */
    public void submitApplication(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer merchantId = JSON.parseObject(params, int.class);
        try {
            Resp<String> stringResp = merchantService.updateMerchantStatus(merchantId, ApplicationStatus.IN_PROGRESS);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 退款
     */
    public void refund(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer orderId = JSON.parseObject(params, int.class);
        try {
            Resp<String> stringResp = orderService.refund(orderId);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 获取房间号
     */
    public void getRoomNumbers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer roomId = JSON.parseObject(params, int.class);
        try {
            Resp<List<RoomNumber>> listResp = roomService.queryRoomNumberByRoomId(roomId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 添加房间号
     */
    public void addRoomNumber(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        Integer roomId = root.getInteger("roomId");
        String roomNumber = root.getString("roomNumber");
        try {
            Resp<String> stringResp = roomService.addRoomNumbers(roomId, roomNumber);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 切换房间号状态
     */
    public void toggleRoomStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer roomNumberId = JSON.parseObject(params, int.class);
        try {
            Resp<String> stringResp = roomService.switchRoomNumberStatus(roomNumberId);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 删除房间号
     */
    public void deleteRoomNumber(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer roomNumberId = JSON.parseObject(params, int.class);
        try {
            Resp<String> stringResp = roomService.deleteRoomNumber(roomNumberId);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 入住
     */
    public void checkIn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer orderId = JSON.parseObject(params, int.class);
        try {
            Resp<String> stringResp = orderService.checkIn(orderId);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 退房
     */
    public void checkOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer orderId = JSON.parseObject(params, int.class);
        try {
            Resp<String> stringResp = orderService.checkOut(orderId);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 拒绝退款
     */
    public void refuseRefund(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer orderId = JSON.parseObject(params, int.class);
        try {
            Resp<String> stringResp = orderService.refuseRefund(orderId);
            resp.getWriter().write(JSON.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }
}
